package cn.fr4nk.crawler.utils;

import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import cn.fr4nk.crawler.proxy.Proxy;

public class WebDriverUtil {

	private static Log log = LogFactory.getLog(WebDriverUtil.class);

	public static WebDriver createHtmlUnit(Proxy proxy) {
		return createHtmlUnit(proxy, true);
	}
	
	public static WebDriver createHtmlUnit(Proxy proxy, boolean enableJs) {
		DesiredCapabilities cap = DesiredCapabilities.htmlUnit();
		cap.setVersion(org.openqa.selenium.remote.BrowserType.FIREFOX);
		cap.setJavascriptEnabled(enableJs);
		setProxy(cap, proxy);
		WebDriver wb = new HtmlUnitDriver(cap);
		return wb;
	}

	private static void setProxy(DesiredCapabilities cap, Proxy proxy) {
		if (proxy != null) {
			org.openqa.selenium.Proxy p = new org.openqa.selenium.Proxy();
			log.debug("Use proxy :" + proxy.getIpPort());
			p.setHttpProxy(proxy.getIpPort());
			cap.setCapability(CapabilityType.PROXY, p);
		}
	}
	

	public static WebDriver createChrome(boolean headless, Proxy proxy, boolean useJs) {
		ChromeOptions options = new ChromeOptions();
		if (headless)
			options.addArguments("--headless");

		DesiredCapabilities cap = DesiredCapabilities.chrome();
		cap.setJavascriptEnabled(useJs);
		cap.setCapability(ChromeOptions.CAPABILITY, options);
		setProxy(cap, proxy);
		WebDriver wb = new ChromeDriver(cap);
		return wb;
	}

	public static WebDriver createChrome(boolean headless, Proxy proxy) {
		ChromeOptions options = new ChromeOptions();
		if (headless)
			options.addArguments("--headless");

		DesiredCapabilities cap = DesiredCapabilities.chrome();
		cap.setJavascriptEnabled(true);
		cap.setCapability(ChromeOptions.CAPABILITY, options);
		setProxy(cap, proxy);
		WebDriver wb = new ChromeDriver(cap);
		return wb;
	}

	public static String randomUserAgent() {
		Random r = new Random(System.currentTimeMillis());
		return userAgents[r.nextInt(userAgents.length)];
	}

	public static String[] userAgents = new String[] {

			"Mozilla/5.0(Macintosh;U;IntelMacOSX10_6_8;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/5.1Safari/534.50",
			"Mozilla/5.0(Windows;U;WindowsNT6.1;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/5.1Safari/534.50",
			"Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0",
			"Mozilla/5.0(Macintosh;IntelMacOSX10.6;rv:2.0.1)Gecko/20100101Firefox/4.0.1",
			"Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1",
			"Opera/9.80(Macintosh;IntelMacOSX10.6.8;U;en)Presto/2.8.131Version/11.11",
			"Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11",
			"Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;Trident/4.0;SE2.XMetaSr1.0;SE2.XMetaSr1.0;.NETCLR2.0.50727;SE2.XMetaSr1.0)",
			"Mozilla/5.0(iPhone;U;CPUiPhoneOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5",
			"Mozilla/5.0(iPod;U;CPUiPhoneOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5",
			"MQQBrowser/26Mozilla/5.0(Linux;U;Android2.3.7;zh-cn;MB200Build/GRJ22;CyanogenMod-7)AppleWebKit/533.1(KHTML,likeGecko)Version/4.0MobileSafari/533.1" };

}
